package com.yuanfeng.business.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.business.dto.ShopNavDTO;
import com.yuanfeng.business.dto.UserTokenBeanDTO;
import com.yuanfeng.business.entity.ShopNavEntity;
import com.yuanfeng.business.mapper.ShopNavMapper;
import com.yuanfeng.business.service.ShopNavService;
import com.yuanfeng.commoms.aspect.UserPermissionsResult;
import com.yuanfeng.commoms.aspect.UserService;
import com.yuanfeng.commoms.config.PermissionsConstant;
import com.yuanfeng.commoms.dto.shop.MerchantShopNavQueryDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ShopNavServiceImpl extends ServiceImpl<ShopNavMapper, ShopNavEntity>
        implements ShopNavService{
    @Autowired
    private ShopNavMapper shopNavMapper;

    /**
     * 根据条件查询店铺导航栏
     * @param shopId
     * @return
     */
    @Override
    public List<MerchantShopNavQueryDTO> selectShopNav(Integer shopId) {
        return this.baseMapper.selectShopNav(shopId);
    }

    @Override
    public ResponseResult deleteshopNav(Map<String, Object> param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP, param.get("token").toString());

        if (userPermissionsResult.getFlag()) {
            this.baseMapper.deleteById(param.get("navId").toString());

        } else {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }
        return ResponseResult.success(BizCodeEnume.DELETE_SUCCESS);
    }

    @Override
    public ResponseResult addShopNav(ShopNavDTO param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP, param.getToken());

        if (userPermissionsResult.getFlag()) {
            param.setShopId(userPermissionsResult.getShopId());
            param.setStatus(1);

            // 店铺导航
            if (null == param.getId()) {
                param.setCreateTime(new Date());
                this.baseMapper.insertSelective(param);
            } else {
                this.baseMapper.updateByPrimaryKeySelective(param);
            }

        } else {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);

        }
        return ResponseResult.success(BizCodeEnume.DELETE_SUCCESS);

    }

    @Override
    public ResponseResult queryshopNav(UserTokenBeanDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getUserInfoByToken(param.getToken());
        if (null == userInfo) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL.getCode(), BizCodeEnume.TOKEN_IS_ILLEGAL.getMsg());
        }
        List<MerchantShopNavQueryDTO>  merchantShopNavQuery = shopNavMapper.queryshopNav(userInfo.getShopId());
        return ResponseResult.success(merchantShopNavQuery);
    }
}
